<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.value" clearable placeholder="输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="query.type" clearable placeholder="类型" class="filter-item" style="width: 130px">
          <el-option v-for="item in queryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="700px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="150px">
          <el-form-item label="类型" prop="type">
            <el-input v-model="form.type" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="标题" prop="title">
            <el-input v-model="form.title" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="动态内容">
            <el-input v-model="form.content" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="图片地址">
              <MaterialList
                      v-model="imgUrlArr"
                      style="width: 915px;"
                      type="image"
                      :num="1"
                      :width="150"
                      :height="150"
              />
          </el-form-item>
          <el-form-item label="缩略图">
              <MaterialList
                      v-model="thumbnailArr"
                      style="width: 915px;"
                      type="image"
                      :num="1"
                      :width="150"
                      :height="150"
              />
          </el-form-item>
          <el-form-item label="视频地址">
              <MaterialList
                      v-model="videoUrlArr"
                      style="width: 915px;"
                      type="image"
                      :num="1"
                      :width="150"
                      :height="150"
              />
          </el-form-item>
          <el-form-item label="省市区" prop="areaId">
            <el-input
              v-if="false"
              v-model="form.areaId"
              style="width: 370px;"
            />
            <div v-if="form.areaId">
              {{ dialogTableDataAreaId.showInputValue }}
            </div>
            <!-- 新增组件弹出式表单 -->
            <dialogTable
              :dialogTableData.sync="dialogTableDataAreaId"
              @select-callback="dialogTableSelectedCallBackAreaId"
            ></dialogTable>
          </el-form-item>
          <el-form-item label="地址">
            <el-input v-model="form.address" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select v-model="form.status" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.CRICLE_STATUS"
                :key="item.id"
                :label="item.label"
                :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="浏览量">
            <el-input v-model="form.browseNumber" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="发布用户" prop="yxUserId">
            <el-input
              v-if="false"
              v-model="form.yxUserId"
              style="width: 370px;"
            />
            <div v-if="form.yxUserId">
              {{ dialogTableDataYxUserId.showInputValue ? dialogTableDataYxUserId.showInputValue : from.username}}
            </div>
            <!-- 新增组件弹出式表单 -->
            <dialogTable
              :dialogTableData.sync="dialogTableDataYxUserId"
              @select-callback="dialogTableSelectedCallBackYxUserId"
            ></dialogTable>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="columns.visible('type')" prop="type" label="类型" />
        <el-table-column v-if="columns.visible('title')" prop="title" label="标题" />
        <el-table-column v-if="columns.visible('content')" prop="content" label="动态内容" />
        <el-table-column v-if="columns.visible('imgUrl')" prop="imgUrl" label="图片地址">
          <template slot-scope="scope">
            <a :href="scope.row.imgUrl" style="color: #42b983" target="_blank">
              <img :src="scope.row.imgUrl" alt="点击打开" class="table-img" />
            </a>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('thumbnail')" prop="thumbnail" label="缩略图">
          <template slot-scope="scope">
            <a :href="scope.row.thumbnail" style="color: #42b983" target="_blank">
              <img :src="scope.row.thumbnail" alt="点击打开" class="table-img" />
            </a>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('videoUrl')" prop="videoUrl" label="视频地址">
            <template slot-scope="scope">
              <a :href="scope.row.videoUrl" style="color: #42b983" target="_blank">
                <img :src="scope.row.videoUrl" alt="点击打开" class="table-img" />
              </a>
            </template>
          </el-table-column>
        <el-table-column v-if="columns.visible('areaId')" prop="areaId" label="省市区" />
        <el-table-column v-if="columns.visible('address')" prop="address" label="地址" />
        <el-table-column v-if="columns.visible('status')" prop="status" label="状态" >
          <template slot-scope="scope">
            {{ dict.label.CRICLE_STATUS[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('memberId')" prop="memberId" label="发布会员ID" />
        <el-table-column v-if="columns.visible('browseNumber')" prop="browseNumber" label="浏览量" />
        <el-table-column v-if="columns.visible('createTime')" prop="createTime" label="创建时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-permission="['admin','tanzerActivityCircle:edit','tanzerActivityCircle:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudTanzerActivityCircle from '@/api/tanzer/tanzerActivityCircle'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import MaterialList from "@/components/material";
import dialogTable from "@/views/components/dialogTable";

// crud交由presenter持有
const defaultCrud = CRUD({ title: '圈子动态', url: 'api/tanzerActivityCircle', sort: 'id,desc', crudMethod: { ...crudTanzerActivityCircle }})
const defaultForm = { id: null, memberId: null, content: null, imgUrl: null, thumbnail: null, videoUrl: null, lng: null, lat: null, areaId: null, address: null, status: null, browseNumber: null, isDel: null, createTime: null, createBy: null, updateBy: null, updateTime: null, title: null }
export default {
  name: 'TanzerActivityCircle',
  components: { pagination, crudOperation, rrOperation, udOperation ,MaterialList,dialogTable},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  dicts: ['CRICLE_STATUS'],
  data() {
    return {
      imgUrlArr:[],thumbnailArr:[],videoUrlArr:[],
      permission: {
        add: ['admin', 'tanzerActivityCircle:add'],
        edit: ['admin', 'tanzerActivityCircle:edit'],
        del: ['admin', 'tanzerActivityCircle:del']
      },
      rules: {
        memberId: [
          { required: true, message: '发布会员ID不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '状态不能为空', trigger: 'blur' }
        ],
        title: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'id', display_name: '表ID，主键，供其他表做外键' },
        { key: 'memberId', display_name: '发布会员ID' },
        { key: 'title', display_name: 'title' }
      ],

      //自定义lov数据源
      dialogTableDataAreaId: {
        title: "省市区",
        dialogVisible: false,
        url: "api/tanzerBaseRegion",
        sort: "id,desc",
        buttonContext: "请选择省市区",
        showInputValue: "",
        queryTypeOptions: [
          { key: 'provinceName', display_name: '省级名称' },
          { key: 'cityName', display_name: '市级名称' },
          { key: 'districtName', display_name: '区级名称' },
        ],
        elTtableColumns: [
          {
            columnProp: "provinceName",
            columnLabel: "省级名称"
          },
          {
            columnProp: "cityName",
            columnLabel: "市级名称"
          },
          {
            columnProp: "districtName",
            columnLabel: "区级名称"
          }
        ]
      },
      dialogTableDataYxUserId: {
        title: "会员列表",
        dialogVisible: false,
        url: "/api/yxUser",
        sort: "uid,desc",
        buttonContext: "请选择会员",
        showInputValue: "",
        queryTypeOptions: [
          { key: "username", display_name: "用户名称" },
          { key: "phone", display_name: "手机号码" }
        ],
        elTtableColumns: [
          {
            columnProp: "username",
            columnLabel: "用户名称"
          },
          {
            columnProp: "nickname",
            columnLabel: "用户昵称"
          },
          {
            columnProp: "phone",
            columnLabel: "手机号码"
          },
        ]
      },


    }
  },
  watch: {
    "imgUrlArr":function(val) {
      if (val) {
        this.form.imgUrl = val.join(",");
      }
    },
    "thumbnailArr":function(val) {
      if (val) {
        this.form.thumbnail = val.join(",");
      }
    },
    "videoUrlArr":function(val) {
      if (val) {
        this.form.videoUrl = val.join(",");
      }
    },
  },
  methods: {
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      const query = this.query
      if (query.type && query.value) {
        this.crud.params[query.type] = query.value
      }else{
        delete this.crud.params.id
        delete this.crud.params.memberId
        delete this.crud.params.title
      }
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      if (form.imgUrl && form.id) {
        this.imgUrlArr = form.imgUrl.split(",");
      }else{
        this.imgUrlArr=[]
      }
      if (form.thumbnail && form.id) {
        this.thumbnailArr = form.thumbnail.split(",");
      }else{
        this.thumbnailArr=[]
      }
      if (form.videoUrl && form.id) {
        this.videoUrlArr = form.videoUrl.split(",");
      }else{
        this.videoUrlArr=[]
      }
    },

    setInputValue(
      dialogTableData,
      showInputValue,
      form,
      id,
      content,
      index,
      value,
      url,
      params
    ) {
      dialogTableData[showInputValue] = "";
      if (form[id]) {
        initData(url, params).then(res => {
          dialogTableData[showInputValue] = res[content][index][value];
        });
      }
    },
  [CRUD.HOOK.afterToEdit](crud, form) {
  },

    //省市区数据源
    dialogTableSelectedCallBackAreaId(row) {
      this.form.areaId = row.id;
      if (this.form.areaId) {
        this.dialogTableDataAreaId.showInputValue = row.areaName;
      }
    },
    dialogTableSelectedCallBackYxUserId(row) {
      this.form.yxUserId = row.uid;
      if (this.form.yxUserId) {
        this.dialogTableDataYxUserId.showInputValue = row.username;
      }
    },
  }
}



</script>

<style scoped>
  .table-img {
    display: inline-block;
    text-align: center;
    background: #ccc;
    color: #fff;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    vertical-align: middle;
    width: 32px;
    height: 32px;
    line-height: 32px;
  }
</style>
